import re
import datetime
import os
import sys

# 清洗爬虫的文章内容
def clean_article_content(content):
    """
    Cleans the crawled article content by removing timestamps, emojis, 
    special symbols, and specific keywords like '图片'.
    """
    # Remove timestamps in the format YYYY-MM-DD HH:MM:SS
    content = re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}', '', content)
    
    # Remove emojis and special symbols
    content = re.sub(r'[^\w\s\u4e00-\u9fff]', '', content)
    
    # Remove specific keywords like '图片'
    content = content.replace('图片', '')
    
    # Remove extra whitespace
    content = re.sub(r'\s+', ' ', content).strip()
    return content
    
# Process a txt file, clean its content, and save the cleaned content to a new file
def process_and_clean_file(input_file_path):
    """
    Reads a txt file, cleans its content using clean_article_content function,
    and saves the cleaned content to a new txt file with a timestamped filename.
    """
    try:
        # Read the content of the input file
        with open(input_file_path, 'r', encoding='utf-8') as file:
            content = file.read()
        # Clean the content
        cleaned_content = clean_article_content(content)
        
        # Generate a new filename with a timestamp
        base_name, ext = os.path.splitext(input_file_path)
        timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        output_file_path = f"{base_name}_{timestamp}{ext}"
        
        # Write the cleaned content to the output file
        with open(output_file_path, 'w', encoding='utf-8') as file:
            file.write(cleaned_content)
        
        print(f"Cleaned content saved to {output_file_path}")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    """
    Example command to run the script:
    python clear_article_by_crawler.py E:/path/to/your/input_file.txt
    """
    if len(sys.argv) != 2:
        print("Usage: python clear_article_by_crawler.py <input_file_path>")
        sys.exit(1)

    input_file_path = sys.argv[1]
    process_and_clean_file(input_file_path)
